<?php
  //This has to be the first line of code a file if sessions are used.
  session_start();
  
  require_once ('check_PDO.php');
  require_once ('appinfo.php');

  if(isset($_SESSION['AppInfo'])) {
    $app = unserialize($_SESSION['AppInfo']);
    if($app->ConnURL == "") {
      header("Location: login.php"); 
      exit; 
    }
  } else {
    header("Location: login.php");
    exit; 
  }

  include_once ('init_db_connect.php');  
  require_once ('DBSchema.php');  
  require_once ('helper.php');  

  try {
    $conn_str = "cubrid:dbname=".$dbConfig['DB_DATABASE_NAME'].";host=".$dbConfig['DB_HOST'].";port=30000";
    $conn = new PDO($conn_str, $dbConfig['DB_USERNAME'], $dbConfig['DB_PASSWORD'] );
  } catch(PDOException $e) {
    header("Location: login.php"); 
  }

  $table_name = "";
  if(isset($_GET['table_name'])) {
    $table_name = $_GET['table_name'];
  } else {
    exit; 
  }  
  
  $row_fk = "";
  $script = "";
  $script = get_table_definition($conn, $table_name, "", false);
  $script = str_replace(" ,", ",", $script);
  $script = "--".PHP_EOL."-- Table ".$table_name.PHP_EOL."--".PHP_EOL.$script.PHP_EOL;
  
  if($script == "") {
    exit; 
  }  
    
  $alt_row_bgcolor = "#F8F8F8"; 
?>
  
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Table definition :: <?php echo $table_name; ?></title>
    <link href='http://alexgorbatchev.com/pub/sh/2.0.296/styles/shCore.css' rel='stylesheet' type='text/css'/> 
    <link rel="stylesheet" href="css/default.css">
    <link rel="stylesheet" href="css/tabber.css">
    <link href='http://alexgorbatchev.com/pub/sh/2.0.296/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/> 
    <script src='http://alexgorbatchev.com/pub/sh/2.0.296/scripts/shCore.js' type='text/javascript'></script>
    <script src='http://alexgorbatchev.com/pub/sh/2.0.296/scripts/shBrushSql.js' type='text/javascript'></script>
    <script type='text/javascript'>
       SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/2.0.296/scripts/clipboard.swf';
       SyntaxHighlighter.all();
    </script> 
    <script type="text/javascript" src="js/tabber-minimized.js"></script>
  </head>
  <body>
    <br />
    <p align="center" class="title2">Table: <font color="red"><?php echo $table_name; ?></font></p>
    <table width="100%">
      <tr>
        <td align="left" valign="top" width="100%" style="margin: 50px;">
          <div class="tabber">
            <div class="tabbertab"  title="Columns">
              <br />
              <table class="popup">
                <tr>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">PK</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Name</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Type</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Auto Increment</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Default value</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Not NULL</b>
                  </th>
                </tr>                
                <?php
                $row_count = 0;
                $row_bgcolor = "#FFFFFF";
                $result = $conn->cubrid_schema(PDO::CUBRID_SCH_ATTRIBUTE, $table_name);
                if($result != null) {
                  foreach ($result as $row) {
                    $row_count++;
                    if($row_count%2 == 0) {
                      $row_bgcolor = "#FFFFFF";
                    } else {
                      $row_bgcolor = $alt_row_bgcolor;
                    }
                ?>
                    <tr bgcolor="<?php echo $row_bgcolor; ?>"> 
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php echo column_is_pk($conn, $table_name, $row["ATTR_NAME"]); ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px; color: red; font-weight: bold;">
                        <?php echo $row["ATTR_NAME"]; ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php echo decode_CUBRID_data_type($conn, $table_name, $row["ATTR_NAME"]); ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php echo decode_CUBRID_auto_increment($conn, $table_name, $row["ATTR_NAME"], false); ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php
                        $default = ""; 
                        if($row["DEFAULT"] != "") {
                          $default = $row["DEFAULT"];
                        }
                        echo $default; 
                        ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php
                        $not_null = ""; 
                        if($row["NON_NULL"] == "1") {
                          $not_null = "Yes";
                        }                        
                        echo $not_null; 
                        ?>
                      </td>
                    </tr>                
                <?php
                    }
                  }
                ?>
              </table>
            </div>
            <div class="tabbertab"  title="Indexes">
              <br />
              <table class="popup">
                <tr>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Column</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Index name</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Type</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Key order</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Index keys count</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Pages count</b>
                  </th>
                </tr>                
                <?php
                $row_count = 0;
                $row_bgcolor = "#FFFFFF";
                $result = $conn->cubrid_schema(PDO::CUBRID_SCH_CONSTRAINT, $table_name);
                if($result != null) {
                  foreach ($result as $row) {
                    $row_count++;
                    if($row_count%2 == 0) {
                      $row_bgcolor = "#FFFFFF";
                    } else {
                      $row_bgcolor = $alt_row_bgcolor;
                    }
                ?>
                    <tr bgcolor="<?php echo $row_bgcolor; ?>"> 
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php echo $row["ATTR_NAME"]; ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px; color: red; font-weight: bold;">
                        <?php echo $row["NAME"]; ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php
                        $index_type = "";
                        if($row["TYPE"] == 0) {
                          if($row["ASC_DESC"] == "A") {
                            $index_type = " UNIQUE INDEX";
                          } else {
                            $index_type = " REVERSE UNIQUE INDEX";
                          }
                        }
                        if($row["TYPE"] == 1) {
                          if($row["ASC_DESC"] == "A") {
                            $index_type = " INDEX";
                          } else {
                            $index_type = " REVERSE INDEX";
                          }
                        }
                        //there is ia bug in CUBRID PDO, values 2 & 3 re not correctly returned
                        if($row["TYPE"] == 2) {
                          $index_type = "REVERSE UNIQUE INDEX";
                        }
                        if($row["TYPE"] == 3) {
                          $index_type = "REVERSE INDEX";
                        }
                        
                        echo $index_type; 
                        ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php echo $row["KEY_ORDER"]; ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php echo $row["NUM_KEYS"]; ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php echo $row["NUM_PAGES"]; ?>
                      </td>
                    </tr>                
                <?php
                    }
                  }
                ?>
              </table>
            </div>
            <div class="tabbertab"  title="Foreign keys">
              <br />
              <table class="popup">
                <tr>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">FK Column</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">FK Name</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Referenced Table</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Referenced Column</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Update rule</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Delete rule</b>
                  </th>
                </tr>                
                <?php
                $row_count = 0;
                $row_bgcolor = "#FFFFFF";
                $result = $conn->cubrid_schema(PDO::CUBRID_SCH_IMPORTED_KEYS, $table_name);
                if($result != null) {
                  foreach ($result as $row) {
                    $row_count++;
                    if($row_count%2 == 0) {
                      $row_bgcolor = "#FFFFFF";
                    } else {
                      $row_bgcolor = $alt_row_bgcolor;
                    }
                ?>
                    <tr bgcolor="<?php echo $row_bgcolor; ?>"> 
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php echo $row["FKCOLUMN_NAME"]; ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px; color: red; font-weight: bold;">
                        <?php echo $row["FK_NAME"]; ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php echo $row["PKTABLE_NAME"]; ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php echo $row["PKCOLUMN_NAME"]; ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php
                        $update_rule = ""; 
                        if($row["UPDATE_RULE"] == 0) {
                          $update_rule = " ON UPDATE CASCADE";
                        }
                        if($row["UPDATE_RULE"] == 1) {
                          $update_rule = " ON UPDATE RESTRICT";
                        }
                        if($row["UPDATE_RULE"] == 3) {
                          $update_rule = " ON UPDATE SET NULL";
                        }
                        echo $update_rule; 
                        ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php
                        $delete_rule = ""; 
                        if($row["DELETE_RULE"] == 0) {
                          $delete_rule = " ON DELETE CASCADE";
                        }
                        if($row["DELETE_RULE"] == 1) {
                          $delete_rule = " ON DELETE RESTRICT";
                        }
                        if($row["DELETE_RULE"] == 3) {
                          $delete_rule = " ON DELETE SET NULL";
                        }
                        echo $delete_rule; 
                        ?>
                      </td>
                    </tr>                
                <?php
                    }
                  }
                ?>
              </table>
            </div>
            <div class="tabbertab"  title="Partition">       
              <br />
              <table class="popup">
                <tr>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Partition name</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Type</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Expression</b>
                  </th>
                  <th style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                    <b style="color: navy;">Expression value</b>
                  </th>
                </tr>                
                <?php
                $row_count = 0;
                $row_bgcolor = "#FFFFFF";
                $sql = "select * from db_partition where class_name='".$table_name."'";
                $result = $conn->query($sql);
                if($result != null) {
                  foreach ($result as $row) {
                    $row_count++;
                    if($row_count%2 == 0) {
                      $row_bgcolor = "#FFFFFF";
                    } else {
                      $row_bgcolor = $alt_row_bgcolor;
                    }
                ?>
                    <tr bgcolor="<?php echo $row_bgcolor; ?>"> 
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php echo $row["partition_name"]; ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px; color: red; font-weight: bold;">
                        <?php echo $row["partition_type"]; ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php echo $row["partition_expr"]; ?>
                      </td>
                      <td style="margin-left: 10px; margin-right: 10px; padding-left: 10px; padding-right: 10px;">
                        <?php echo $row["partition_values"]; ?>
                      </td>
                    </tr>                
                <?php
                    }
                  }
                ?>
              </table>
            </div>
            <div class="tabbertab" title="SQL Script">
              <br />
              <table>
                <tr>
                  <td>
                    <pre class="brush:[sql]"><?php echo $script; ?></pre> 
                  </td>
                </tr>                
              </table>
            </div>
          </div>
        </td>
      </tr>
    </table>    
    </form>
    <?php
    $conn = null; //disconnect
    ?>  
  </body>
</html>